/**   
* @Title: ClientMain.java
* @Package com.an.nettyl
* @Description: TODO(用一句话描述该文件做什么)
* @author antch,861710865@qq.com   
* @date 2017年8月3日 上午8:49:41
* @version V1.0   
*/


package com.an.nettyl;


import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
* @ClassName: ClientMain
* @Description: TODO(客户端启动类)
* @author antch,861710865@qq.com
* @date 2017年8月3日 上午8:49:41
* 
*/

public class ServerMain {

	/** 
	* @Title: main 
	* @Description: TODO(客户端启动主线程) 
	* @param @param args    设定文件 
	* @return void    返回类型 
	* @throws 
	*/

	public static void main(String[] args) {
		EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)
        EventLoopGroup workerGroup = new NioEventLoopGroup(); // (2)
        int port = 8867;
        try {
            ServerBootstrap b = new ServerBootstrap(); // (3)
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class) // (4)
                    .childHandler(new ChannelInitializer<SocketChannel>() { // (5)
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                 
                    ch.pipeline().addLast(new ServerHandler());
                    
                    
                }
            })
            .option(ChannelOption.SO_BACKLOG, 128)          // (6)
            .childOption(ChannelOption.SO_KEEPALIVE, true).childOption(ChannelOption.TCP_NODELAY, true); // (7)

            // Bind and start to accept incoming connections.
            ChannelFuture f = b.bind(port).sync(); // (8)

            // Wait until the server socket is closed.
            // In this example, this does not happen, but you can do that to gracefully
            // shut down your server.
            
            System.out.println("start server....");
            f.channel().closeFuture().sync();
            System.out.println("stop server....");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
            System.out.println("exit server....");
        }
	}

}
